package demo.practice.leetcode_eveyday;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class P90 {
    //[1]   [] [1]
    //[1,2]     [] [1]  [2] [1,2]
    //[1,2,2]   [] [1]  [2] [2,1]  [2] [1,2] [2,2] [2,1,2] 去重得到        [] [1] [2] [1,2]   [2,2] [2,1,2]



    public static void main(String[] args) {
        P90 p90= new P90();
        List<List<Integer>> lists = p90.subsetsWithDup(new int[]{1, 2, 3});
        System.out.println("xx");

    }
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        List<Integer> all = new ArrayList<>();
        Arrays.stream(nums).forEach(
                i -> all.add(i)
        );
        subsetsWithDup(list, all);
        return list;
    }

    public void subsetsWithDup(List<List<Integer>> list, List<Integer> all) {
        if (all.size() == 0){
            list.add(new ArrayList<>());
            return ;
        }


        subsetsWithDup(list, all.subList(0,all.size()-1));
        int cur= all.get(all.size()-1);//拿到最后一个元素
        boolean exist= all.subList(0,all.size()-1).contains(cur);

        List<List<Integer>> listNew = new ArrayList<>();

        list.forEach(
                i->{
                     if(!exist||i.contains(cur)){//前面不包含该元素，则将
                         List addlist=new ArrayList<>(i);
                         addlist.add(cur);
                         listNew.add(addlist);
                     }
                }
        );
        list.addAll(listNew);
    }


}
